<?php 
class Event extends AppModel
{
    var $name = 'Event';
   	var $validate = array(
        'title'=>array(
            'notempty'=>array(
                'rule'=>'notempty',
                'message'=>'Title must be present'
             ),
             'between'=>array(
                'rule'=>array('between', '6', '50'),
                'message'=>'Title must be between 6 to 50 character'
             )
        ), 'start_date'=>array(
            'rule' =>'validate_start_date'
        ), 'end_date'=>array(
            'rule'=>'validate_end_date'
        ), 'creator_id'=>array(
            'numeric'
        ), 'attendance_manager_id'=>array(
            'numeric'
        ), 'event_type_id'=>array(
            'numeric'
        ), 'season_id'=>array(
            'numeric'
        ), 'is_closed'=>array(
            'boolean'
        ), 'is_deleted'=>array(
            'boolean'
        )
    );
    
    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $belongsTo = array(
        'Creator'=>array(
            'className'=>'User',
            'foreignKey'=>'creator_id',
            'conditions'=>'',
            'fields'=>'',
            'order'=>''
        ), 'AttendanceManager'=>array(
            'className'=>'User',
            'foreignKey'=>'attendance_manager_id',
            'conditions'=>'',
            'fields'=>'',
            'order'=>''
        ), 'EventType'=>array(
            'className'=>'EventType',
            'foreignKey'=>'event_type_id',
            'conditions'=>'',
            'fields'=>'',
            'order'=>''
        ), 'Season'=>array(
            'className'=>'Season',
            'foreignKey'=>'season_id',
            'conditions'=>'',
            'fields'=>'',
            'order'=>''
        )
    );
    
    var $hasAndBelongsToMany = array(
        'Instructor'=>array(
            'className'=>'User',
            'joinTable'=>'event_instructors',
            'foreignKey'=>'event_id',
            'associationForeignKey'=>'user_id'
        )
    );

    
    var $hasMany = array(
        'EventPrivilage' => array(
            'className' => 'EventPrivilage', 
            'foreignKey' => 'event_id', 
            'dependent' => true
        ),
        'Comment' => array(
			'className' => 'Comment', 
        	'foreignKey' => 'parent_id',
        	'dependent' => true
        ),
        'EventAttendee' => array(
        	'className' => 'EventAttendee',
        	'foreignKey' => 'event_id',
        	'dependent' => true
        )
    );

    
    function validate_start_date($date)
    {
        $date = $date['start_date'];
        if ($this->validate_date($date))
        {
            $today = date('Y-m-d');
            $today = strtotime($today);
            $start = strtotime($date);
            if ($start > $today)
            {
                return true;
            }
        }
        return false;
    }
    
    function validate_end_date($date)
    {
        $startDate = $this->data['Event']['start_date'];
        $endDate = $date['end_date'];
        if ($this->validate_date($endDate))
        {
            $start = strtotime($startDate);
            $end = strtotime($endDate);
            if ($end > $start)
            {
                return true;
            }
        }
        return false;
    }
}
?>
